using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcJ.Models;
using System.IO;

namespace MvcJ.Controllers
{
    public class JQuerySampleController : Controller
    {
        //
        // GET: /JQuerySample/

        public ActionResult Index()
        {
            return View();
        }

        #region Get & Post
        public ActionResult Sample1()
        {
            SimpleService service = new SimpleService();
            var list = service.ListModels();
            if (Request.IsAjaxRequest())
            {
                //Ajax request 
                return PartialView("ModelList", list);
            }
            //normal request 
            return View(list);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Get()
        {
            SimpleService service = new SimpleService();
            //Ajax request
            SimpleModel model = new SimpleModel();
            UpdateModel(model);
            service.SaveModel(model); 
            return Json("OK!");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Get2()
        {
            SimpleService service = new SimpleService();
            //Ajax request
            SimpleModel model = new SimpleModel();
            UpdateModel(model);
            service.SaveModel(model);
            var list = service.ListModels();
            return PartialView("ModelList", list);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Get2_WithDeleteLink()
        {
            SimpleService service = new SimpleService();
            //Ajax request
            SimpleModel model = new SimpleModel();
            UpdateModel(model);
            service.SaveModel(model);
            var list = service.ListModels();
            return PartialView("ModelList_WithDeleteLink", list);
        }

        public ActionResult Sample2()
        {
            SimpleService service = new SimpleService();
            var list = service.ListModels(); 
            //normal request 
            return View(list);
        }

        public ActionResult AjaxDelete(int? id)
        {
            SimpleService service = new SimpleService();
            //Ajax request 
            int  idToDelete = id ?? -1;
            service.DeleteModel(idToDelete);
            var list = service.ListModels();
            return PartialView("ModelList_WithDeleteLink", list);
        }

        #endregion

        #region Ajax Upload

        public ActionResult AjaxUpload()
        {
            if (Request.IsAjaxRequest())
            {
                string strFileName = Path.GetFileName(Request.Files[0].FileName);
                string strExtension = Path.GetExtension(Request.Files[0].FileName).ToLower();
                string strSaveLocation = Server.MapPath("~/uploadfiles/" + strFileName);
                Request.Files[0].SaveAs(strSaveLocation);
                ViewData["result"] = "OK";
                return Content("Success");
            }
            return View();
        }

        public ActionResult AjaxUploadFile()
        { 
                string strFileName = Path.GetFileName(Request.Files[0].FileName);
                string strExtension = Path.GetExtension(Request.Files[0].FileName).ToLower();
                string strSaveLocation = Server.MapPath("~/uploadfiles/" + strFileName);
                Request.Files[0].SaveAs(strSaveLocation);
                ViewData["result"] = "OK";
                return Content("Success"); 
        }
        #endregion

    }
}
